<?php 
class catesAction extends AdminMaster{
	private $strClass = 0;
	public function execute($request){
		$this->data['navigation']	= __('module_faqs',null,'menu').' › '. __('module_faqs_action_faq_cates',null,'menu');
		$this->formData				= $request->getParameter('FaqCates');	//get form data from add new || edit page
		
		//$this->data['list']['cates'] = ;
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list']			 		= FaqCates::getAllCates(true);//FaqCatesTable::getDataList($this->data['filter'], $this->data['pagination']);
		$this->data['list_total']			= 0;//FaqCatesTable::getDataListCount($this->data['filter']);
		$pag	= new PaginationHelper($this->data['list_total'], $this->data['pagination']['limit'], $this->data['pagination']['page_show'], $this->data['pagination']['page'], 'faq_cates');
		$this->data['pagination_pager']	= $pag->getPaginations();
		
		$strRow = '';
		foreach($this->data['list'] as $cate){
			$strRow.=self::getListMultiLevel($cate['id'], $cate['children']);
		}
		$this->data['list']['data'] = $strRow;
	}
	private function getListMultiLevel($id = -1, $childs = array(), $level = 0){
		$data = FaqCatesTable::getDataById($id, true);//print_r($data);
		if(!is_array($data) || count($data)<=0 || intval($data['id'])==intval($currentId)){
			return '';
		}
		$str = '<tr class="class'.intval(++$this->strClass%2).' enable-'.intval($data['is_enabled']).'">
					<td class="checkbox-select"><input type="checkbox" name="select_list[]" class="select_list" value="'.$id.'" id="select_'.$id.'" title="'. __('to_check',null,'table_list').'" /></td>
					<td class="name">'.ArrayHelper::getStringHeadOptionLevel($level,'&nbsp;&nbsp;&nbsp;').
						'<a href="#" class="a-control-get edit" data-action="'.ADMIN_DEFINED::EDIT_RECORD.'&id='.$id.'" data-require="none">
							'.$data['name'].'
						</a>
					</td>
					<td class="sort-order">
						<input type="text" data-require="number-only" name="sort_orders[]" class="sort-order" value="'.$data['sort_order'].'"/>
						<input type="hidden" name="ids[]" value="'.$id.'"/>
					</td>
					<td class="tags">'.$data['tags'].'</td>
					<td class="updated-by">'.$data['updated_by'].'</td>
					<td class="updated-at">'.$data['updated_at'].'</td>
					<td class="action">';
		$sepa = ''; 
		foreach ($this->data['action_list'] as $action){
			$str.=$sepa.' <a href="'.sprintf($action['href'], $data['id']).'" title="'.$action['title'].'" alt="'.$action['title'].'" class="column-action">'.$action['text'].'</a>';
			$sepa = '|';
		}
		$str.='			</td>
				</tr>';
		foreach($childs as $val){
			$str.= self::getListMultiLevel($val['id'], $val['children'], $level+1);
		}
		
		return $str;
	}
	private function delRecord(){
		//checking user
		FaqCatesTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		//checking user
		FaqCatesTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		//checking user
		FaqCatesTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		FaqCates::saveSortOrder($sortData);
		$this->filterList();
	}
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('faqCatesForm');
		
		$form = new FaqCatesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$product_id = FaqCatesTable::insert($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success', null, 'faq_cates');
					$form = new FaqCatesFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
				$this->data['cate_options']	= ArrayHelper::getStringOptionMultiLevel(array('id'=>'0', 'name'=>'--Root--','level'=>0,'children'=>FaqCates::getAllCates()));
					
			}else{
				$this->data['cate_options']	= ArrayHelper::getStringOptionMultiLevel(array('id'=>'0', 'name'=>'--Root--','level'=>0,'children'=>FaqCates::getAllCates()), $this->formData['cate_id']);
			}
		}else{
			$this->data['cate_options']	= ArrayHelper::getStringOptionMultiLevel(array('id'=>'0', 'name'=>'--Root--','level'=>0,'children'=>FaqCates::getAllCates()));
			
		}
		$this->setVar('form', $form);
	}

	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('faqCatesForm');

		$form = new FaqCatesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(FaqCatesTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'faq_cates');
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
			}
			$this->data['cate_options']	= ArrayHelper::getStringOptionMultiLevel(array('id'=>'0', 'name'=>'--Root--','level'=>0,'children'=>FaqCates::getAllCates()), $this->formData['cate_id'], $this->data['id']);
		}else{
			$data = FaqCatesTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
						'name'				=> $data['name'],
						'cate_id'			=> $data['cate_id'],
						'description'		=> $data['description'],
						'tags'				=> $data['tags'],
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$form->bind($this->formData);

			}
			$this->data['cate_options']	= ArrayHelper::getStringOptionMultiLevel(array('id'=>'0', 'name'=>'--Root--','level'=>0,'children'=>FaqCates::getAllCates()), $data['cate_id'], $this->data['id']);
		}
		$this->setVar('form', $form);

	}
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
}